//var code = null
jQ(function(){
//	code = getParameter("code");
//	if(code==null){
//		window.location.href = "/index.html";
//	}else{
		var div_box = jQ("#register").find("div");
		jQ.each(div_box,function(i,dat){
			initBulidFun(dat);
		});
//		initApplyUser(code);
//	}
});
function initApplyUser(code){
	jQ.ajax({
		type: "POST",
		url: "userRegApplyAjax!getApplyUser.action",
		data:{
			vcode : code
		},
		success:function(data){
			var dataObj = eval("("+data+")");
			if(dataObj.flag==1){
				jQ("#register div.email input").val(dataObj.email);
				jQ("#register div.real_name input").val(dataObj.name);
			}else{
				jalert(dataObj.msg,function(){
					window.location.href = "/index.html";
				});
			}
		}
	});
}
function checkEmail(_input,_b,fun){
	var _value = jQ.trim(_input.val());
	if(isEmail(_value)){
		jQ.ajax({
			type: "POST",
			url: "userAjax!checkUser.action",
			async: false,
			data:{
				emailInput : _value
			},
			success:function(data){
				_b.removeClass().empty();
				if(data=="0"){
					_b.addClass("wrong").html("邮箱地址已被使用,请重新输入");
				}else{
					_b.addClass("right");
				}
				if(typeof(fun)=="function")
					fun();
			}
		});
	}else{
		_b.removeClass().addClass("wrong").html("请输入正确的邮箱地址.");
		if(typeof(fun)=="function")
			fun();
	}
}
function checkPWD(_input,_b,fun){
	var regEx = /^[a-zA-Z0-9][a-zA-Z0-9_-]{5,19}$/;
	var _value = jQ.trim(_input.val());
	_b.removeClass().empty();
	if(!regEx.test(_value)){
		_b.addClass("wrong").html("数字,大小写字母6到30位以上组合的密码");
	}else{
		_b.addClass("right");
	}
	if(typeof(fun)=="function")
		fun();
}
function checkPWD2(_input,_b,fun){
	var pwd = _input.parent().prev("div.password").find("input").val();;
	var _value = jQ.trim(_input.val());
	_b.removeClass().empty();
	if(_value.length<=0){
		_b.addClass("wrong").html("请输入密码");
	}else if(pwd==_value){
		_b.addClass("right");
	}else{
		_b.addClass("wrong").empty().append("密码与再次重复密码不一致.");
	}
	if(typeof(fun)=="function")
		fun();
}
function checkName(_input,_b,fun){
	var regEx = /[A-Za-z0-9\u4e00-\u9fa5]{2,20}$/;//汉字
	var _value = jQ.trim(_input.val());
	_b.removeClass().empty();
	if(!regEx.test(_value)){
		_b.addClass("wrong").html("姓名不在5-20个字符以内或包含非法字符,请重新输入.");
	}else{
		_b.addClass("right");
	}
	if(typeof(fun)=="function")
		fun();
}
function checkLive(_input,_b,fun){
	var _value = jQ.trim(_input.val());
	_b.removeClass().empty();
	if(_value.length>15){
		_b.addClass("wrong").html("输入内容不能大于15个字符");
	}else if(_value.length>0){
		_b.addClass("right")
	}
	if(typeof(fun)=="function")
		fun();
}
function checkBlog(_input,_b,fun){
	var _value = jQ.trim(_input.val());
	_b.removeClass().empty();
	if(_value.length>0){
		if(IsURL(_value)){
			_b.addClass("right")
		}else{
			_b.addClass("wrong").html("请输入以http://开头的URL");
		}
	}
	if(typeof(fun)=="function")
		fun();
}
function checkYzm(_input,_b,fun){
	var _value = jQ.trim(_input.val());
	_b.removeClass().empty();
	if(_value.length<0){
		_b.addClass("wrong").html("请输入验证码");
		if(typeof(fun)=="function")
			fun();
	}else{
		jQ.ajax({
			type: "POST",
			url: "userAjax!checkYzm.action",
			async: false,
			data:{
				vcodeInput : _value
			},
			success:function(data){
				if(data=="0"){
					_b.addClass("wrong").html("验证码错误,请重新输入");
				}else{
					_b.addClass("right");
				}
				if(typeof(fun)=="function")
					fun();
			}
		});
	}
}
function initBulidFun(dat){
	var _input = jQ(dat).find("input");
	var _b = jQ(dat).find("b");
	
	if(jQ(dat).hasClass("email")){
		_input.blur(function(){
			checkEmail(_input,_b);
		});
	}else if(jQ(dat).hasClass("password")){
		_input.blur(function(){
			checkPWD(_input,_b);
		});
	}else if(jQ(dat).hasClass("password_repeat")){
		_input.blur(function(){
			checkPWD2(_input,_b);
		});
	}else if(jQ(dat).hasClass("real_name")){
		_input.blur(function(){
			checkName(_input,_b);
		});
	}else if(jQ(dat).hasClass("birthday")){
		_input.datepicker({
			yearRange : '1950',
			dateFormat:'yy-mm-dd',
			changeMonth:true,
			changeYear:true,
			beforeShow: function(i,e){  
				var z = jQuery(i).closest(".ui-dialog").css("z-index") + 4;  
				e.dpDiv.css('z-index', z);
				e.dpDiv.css('font', "14px \"Trebuchet MS\", sans-serif");
			}
		});
	}else if(jQ(dat).hasClass("living")){
		_input.blur(function(){
			checkLive(_input,_b);
		});
	}else if(jQ(dat).hasClass("blog")){
		_input.blur(function(){
			checkBlog(_input,_b);
		});
	}else if(jQ(dat).hasClass("wrap_numb")){
		var _img = jQ(dat).find("img");
		var _a = jQ(dat).find("a");
		_img.click(function(){
			_img.attr("src", jQ("#ctxVal").val()+"/verifycode.do" + "?time=" + (new Date()).valueOf());
		}).click();;
		_a.click(function(){
			_img.attr("src", jQ("#ctxVal").val()+"/verifycode.do" + "?time=" + (new Date()).valueOf());
		});
	}else if(jQ(dat).hasClass("wrap")){
		_input.blur(function(){
			checkYzm(_input,_b);
		});
	}else if(jQ(dat).hasClass("register_btm")){
		var _a = jQ(dat).find("a.rt").click(doSubmit);
	}
}
function doSubmit(){
	var div_box = jQ("#register");
	var _email = div_box.find("div.email");
	var _pwd = div_box.find("div.password");
	var _pwd2 = div_box.find("div.password_repeat");
	var _name = div_box.find("div.real_name");
	var _wrap_numb = div_box.find("div.wrap_numb");
	var _wrap = div_box.find("div.wrap");
	var _register_btm = div_box.find("div.register_btm");
	
	var _birthday = div_box.find("div.birthday");
	var _living = div_box.find("div.living");
	var _job = div_box.find("div.job");
	var _edu = div_box.find("div.edu");
	var _blog = div_box.find("div.blog");
	var _sex = div_box.find("div.sex");
	
//	_input
	var _input_email = _email.find("input");
	var _input_pwd = _pwd.find("input");
	var _input_pwd2 = _pwd2.find("input");
	var _input_name = _name.find("input");
	var _input_wrap = _wrap.find("input");
	var _input_birthday = _birthday.find("input");
	var _input_living = _living.find("input");
	var _select_job = _job.find("select");
	var _select_edu = _edu.find("select");
	var _input_blog = _blog.find("input");
	var _input_sex = _sex.find("input[name='sex']:checked");
//	_b
	var _b_email = _email.find("b");
	var _b_pwd = _pwd.find("b");
	var _b_pwd2 = _pwd2.find("b");
	var _b_name = _name.find("b");
	var _b_wrap = _wrap.find("b");
	var _b_living = _living.find("b");
	var _b_blog = _blog.find("b");
			
	checkEmail(_input_email,_b_email,function(){
		checkPWD(_input_pwd,_b_pwd,function(){
			checkPWD2(_input_pwd2,_b_pwd2,function(){
				checkName(_input_name,_b_name,function(){
					checkYzm(_input_wrap,_b_wrap,function(){
						checkLive(_input_living,_b_living,function(){
							checkBlog(_input_blog,_b_blog,function(){
								var _flag = false;
								jQ.each(div_box.find("div"),function(i,dat){
									var _b = jQ(dat).find("b")
									if(_b.hasClass("wrong")){
										_flag = true;
									}
								})
								if(_flag){
									jalert("请输入正确的信息");
								}else{
									jQ.ajax({
										type : "POST",
										url : "userAjax!register.action",
										data : {
											emailInput : _input_email.val(),
											pwdInput : _input_pwd.val(),
											pwdInput2 : _input_pwd2.val(),
											nameInput : _input_name.val(),
											vcodeInput : _input_wrap.val()
										},
										success : function(data){
											var dataObj=eval("("+data+")");
											if(dataObj.flag==1){
												jQ("#div_msg div.msg p").html(dataObj.result);
												jQ("#div_msg div.msg_btm").html("<input type='button' style='width:auto' value='首页' onclick='gotoLogin()'>");
												jQ("#div_msg").show();
												jQ("#register").hide();
											}else{
												jalert(dataObj.result);
											}
										}
									});
								}
							});
						});
					});
				});
			});
		});
	});
}
function gotoLogin(){
	window.location.href=jQ("#ctxVal").val()+"/index.html";
}
function getParameter(name){
    var paramStr=location.search;
    if(paramStr.length==0)return null;
    if(paramStr.charAt(0)!='?')return null;
    paramStr=decodeURIComponent(paramStr);
    paramStr=paramStr.substring(1);
    if(paramStr.length==0)return null;
    var params=paramStr.split('&');
    var p = null;
    for(var i=0;i<params.length;i++){
        if(params[i].indexOf(name) >= 0){
            p = params[i].split('=');
            p = p[1];
        }
    }
    return p;
}
function jalert(info,LFun){
	jQ("#ALERT p").text(info);
	if(LFun!=null)jQ("#ALERT").bind("LFun",LFun);
	jQ("#ALERT").dialog('open');
	jQ("#ALERT").dialog("option","buttons",{
		"确定" : function(){
			jQ(this).trigger("LFun");
			jQ(this).dialog("close");
		}
	});
}
function IsURL(str_url) {
	var strRegex = "^(http://)+(([0-9a-zA-Z_]+[\.]+[0-9a-zA-Z_])?([0-9a-zA-Z_]))+[\.]+([a-zA-Z]{2,3})+.+$";
	var re = new RegExp(strRegex);
	return re.test(str_url);
}